/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.web.io;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * a wrapper around an {@link HttpServletRequest} that can handle a change in
 * servlet path.
 */
public class SubServletHttpServletRequest
    extends HttpServletRequestWrapper
{

  /**
   * Returns any extra path information associated with the URL the client sent
   * when it made this request. The extra path information follows the servlet
   * path but precedes the query string and will start with a "/" character.
   * <p>
   * This method returns <code>null</code> if there was no extra path
   * information.
   * <p>
   * Same as the value of the CGI variable PATH_INFO.
   * 
   * @return a <code>String</code>, decoded by the web container, specifying
   *         extra path information that comes after the servlet path but before
   *         the query string in the request URL; or <code>null</code> if the
   *         URL does not have any extra path information
   * @see javax.servlet.http.HttpServletRequest#getPathInfo()
   */
  @Override
  public String getPathInfo()
  {
    String pathInfo = super.getPathInfo();
    String completePath = super.getServletPath() + (pathInfo == null ? "" : pathInfo);
    if (completePath.startsWith(servletPath)) {
      return completePath.substring(servletPath.length());
    }
    return pathInfo;
  }

  /**
   * the mimicked path of the servlet.
   */
  private String servletPath;



  /**
   * constructor.
   * 
   * @param request
   *          the {@link HttpServletRequest} to wrapp
   * @param servletPath
   *          the mimicked path of the servlet
   */
  public SubServletHttpServletRequest(HttpServletRequest request, String servletPath)
  {
    super(request);
    this.servletPath = servletPath;
  }



  /**
   * Returns the part of this request's URL that calls the servlet. This path
   * starts with a "/" character and includes either the servlet name or a path
   * to the servlet, but does not include any extra path information or a query
   * string. Same as the value of the CGI variable SCRIPT_NAME.
   * <p>
   * This method will return an empty string ("") if the servlet used to process
   * this request was matched using the "/*" pattern.
   * 
   * @return a <code>String</code> containing the name or path of the servlet
   *         being called, as specified in the request URL, decoded, or an empty
   *         string if the servlet used to process the request is matched using
   *         the "/*" pattern.
   * @see javax.servlet.http.HttpServletRequest#getServletPath()
   */
  @Override
  public String getServletPath()
  {
    return servletPath;
  }



  /**
   * setter for the servletPath property.
   * 
   * @param servletPath
   *          the servletPath to set
   */
  public void setServletPath(String servletPath)
  {
    this.servletPath = servletPath == null ? "" : servletPath;
  }

}
